﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using GioHangViet.Models;

namespace GioHangViet.Areas.Admin.Controllers
{ 
    public class PromotionController : Controller
    {
        private GoLungTungModel db = new GoLungTungModel();

        //
        // GET: /Admin/Promotion/

        public ViewResult Index()
        {
            return View(db.Promotions.ToList());
        }

        //
        // GET: /Admin/Promotion/Details/5

        public ViewResult Details(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            return View(promotion);
        }

        //
        // GET: /Admin/Promotion/Create

        public ActionResult Create()
        {
            ViewBag.Categories = new SelectList(db.Categories.OrderBy(item => item.Alias).ToList(), "Alias", "CategoryName");
            return View();
        } 

        //
        // POST: /Admin/Promotion/Create

        [HttpPost]
        public ActionResult Create(Promotion promotion)
        {
            if (ModelState.IsValid)
            {
                db.Promotions.Add(promotion);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(promotion);
        }
        
        //
        // GET: /Admin/Promotion/Edit/5
 
        public ActionResult Edit(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            return View(promotion);
        }

        //
        // POST: /Admin/Promotion/Edit/5

        [HttpPost]
        public ActionResult Edit(Promotion promotion)
        {
            if (ModelState.IsValid)
            {
                db.Entry(promotion).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(promotion);
        }

        //
        // GET: /Admin/Promotion/Delete/5
 
        public ActionResult Delete(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            return View(promotion);
        }

        //
        // POST: /Admin/Promotion/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Promotion promotion = db.Promotions.Find(id);
            db.Promotions.Remove(promotion);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public JsonResult GetDistrict(string alias)
        {
            var listProduct = (from p in db.Products join cp in db.CategoryProducts on p.Id equals cp.ProductId
                                   join c in db.Categories on cp.CategoryAlias equals c.Alias
                               where c.Alias == alias
                               orderby p.Name select new { p.Alias, p.Name }).ToList();

            return Json(new { ListProduct = listProduct },
                        JsonRequestBehavior.AllowGet);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}